package com.mfin.bs.mappers.impl;

import java.util.Set;

import com.mfin.bom.interfaces.ICustomer;
import com.mfin.bom.interfaces.IFinanceLoan;
import com.mfin.bom.interfaces.ILoanMaster;
import com.mfin.bom.interfaces.IPartner;
import com.mfin.vo.CustomerVO;
import com.mfin.vo.GuarantorListVO;
import com.mfin.vo.GuarantorVO;
import com.mfin.vo.PartnerVO;
import com.mfin.vo.interfaces.IMappable;
import com.mfin.vo.loan.FinanceLoanVO;
import com.mfin.vo.loan.LoanApplicationVO;

public class LoanMapper extends BaseMapper {

	@Override
	protected boolean doMap(IMappable fromArg, IMappable toArg) {
		boolean canPerformMap = false;

		if (fromArg instanceof IFinanceLoan && toArg instanceof FinanceLoanVO) {
			canPerformMap = true;
			performMap((IFinanceLoan) fromArg, (FinanceLoanVO) toArg);
		} else if (fromArg instanceof LoanApplicationVO
				&& toArg instanceof IFinanceLoan) {
			performMap((LoanApplicationVO) fromArg, (IFinanceLoan) toArg);
			canPerformMap = true;
		}
		return canPerformMap;
	}

	private void performMap(LoanApplicationVO fromArg, IFinanceLoan toArg) {
		toArg.setLoanDate(fromArg.getLoanDate());
		toArg.setLoanAmount(fromArg.getLoanAmount());
		toArg.setDuration(fromArg.getDuration());
		toArg.setInterest(fromArg.getInterest());
		toArg.setDocCharges(fromArg.getDocCharges());
		toArg.setDuedate(fromArg.getDuedate());
		toArg.setInstallment(fromArg.getInstallment());
		toArg.setSecurity(fromArg.getSecurity());
		toArg.setSecDetails(fromArg.getSecDetails());
		toArg.setLoanStatus(fromArg.getLoanStatus());
	}

	private void performMap(IFinanceLoan fromArg, FinanceLoanVO toArg) {

		toArg.setLoanId(fromArg.getLoanId());
		if (fromArg.getCustomer() != null) {
			ICustomer bomCustomer = fromArg.getCustomer();
			CustomerVO customerVO = new CustomerVO();
			map(bomCustomer, customerVO, false, true);
		}

		if (fromArg.getPartner() != null) {
			IPartner bomPartner = fromArg.getPartner();
			PartnerVO partnerVo = new PartnerVO();
			map(bomPartner, partnerVo, false, true);
		}

		ILoanMaster loanMaster = fromArg.getLoanmaster();
		toArg.setLoanType(loanMaster.getLoanType());

		toArg.setLoanDate(fromArg.getLoanDate());
		toArg.setLoanAmount(fromArg.getLoanAmount());
		toArg.setDuration(fromArg.getDuration());
		toArg.setInterest(fromArg.getInterest());
		toArg.setDocCharges(fromArg.getDocCharges());
		toArg.setDuedate(fromArg.getDuedate());
		toArg.setInstallment(fromArg.getInstallment());
		toArg.setSecurity(fromArg.getSecurity());
		toArg.setSecDetails(fromArg.getSecDetails());
		toArg.setLoanStatus(fromArg.getLoanStatus());

		if (fromArg.getGuarantors() != null) {
			Set<ICustomer> guarators = fromArg.getGuarantors();
			GuarantorVO[] guarantorArray = new GuarantorVO[guarators.size()];
			int index = 0;
			for (ICustomer bomGuarator : guarators) {
				map(bomGuarator, guarantorArray[index++], false, true);
			}
			toArg.setGuarantors(new GuarantorListVO(guarantorArray));
		}

		// fromArg.getVehicle(); toArg.setVehicle(VehicleVO)
	}
}
